import { wxPost } from '@/api/index'
const actions = {
  SetActiveColumn({ commit, state }, param) {
    commit('SET_ACTIVE_COLUMN', param)
  },
   SetProductColumn({ commit, state }, param) {
    commit('SET_PRODUCT_COLUMN', param)
  },
  SetNewsColumn({ commit, state }, param) {
    commit('SET_NEWS_COLUMN', param)
  },
  SetAboutColumn({ commit, state }, param) {
    commit('SET_ABOUT_COLUMN', param)
  }, 
  GetTabBar({ commit, state }) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=comm&a=domenu_column_id')
        .then(response => {
          if(!state.app_tabBar.index){
            response.data.product.bigclass=response.data.product.column_id
            response.data.news.bigclass=response.data.news.column_id
            response.data.about.bigclass=response.data.about.column_id
            commit('SET_APP_WEB_NAME', response.data.index.web_name)
            commit('SET_APP_TAB_BAR', response.data)
            commit('SET_PRODUCT_COLUMN', response.data.product)
            commit('SET_NEWS_COLUMN', response.data.news)
            commit('SET_ABOUT_COLUMN', response.data.about)
            commit('SET_APP_VIEW_HEIGHT', window.screen.availHeight)
          }

          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetBanner({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=comm&a=dobanner_list', param)
        .then(response => {
          commit('SET_BANNER', response.data)
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetIndexData({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=index&a=doindex', param)
        .then(response => {
         
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetAboutList({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=introduction&a=doindex', param)
        .then(response => {
          commit('SET_ABOUT', response.data.list)
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetMessageList({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=introduction&a=domessage_list', param)
        .then(response => {
          commit('SET_MESSAGE_LIST', response.data.list)
          response.data.form.map(item=>{
            if(item.type==='6'||item.type==='4'||item.type==='2'){
              item.value=[]
              if(item.type==='2'){
                item.value.push(item.list[0].value)
              }
      
              item.list.map(e=>{
                e.key=e.value
                e.name=e.value
              })
            }
            if(item.type==='1'||item.type==='8'||item.type==='9'||item.type==='3'){
              item.value=''
            }
          })
          commit('SET_MESSAGE_FORM', response.data.form)

          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetFeedbackList({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=introduction&a=dofeedback_form', param)
        .then(response => {
          response.data.form.map(item=>{
            if(item.type==='6'||item.type==='4'||item.type==='2'){
              item.value=[]
              if(item.type==='2'){
                item.value.push(item.list[0].value)
              }
      
              item.list.map(e=>{
                e.key=e.value
                e.name=e.value
              })
            }
            if(item.type==='1'||item.type==='8'||item.type==='9'||item.type==='3'){
              item.value=''
            }
          })
          commit('SET_FEEDBACK', response.data)
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetNewsList({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=news&a=donews_list', param)
        .then(response => {
          response.data.list.map(item => {
            item.addtime = item.addtime.substring(0, 10)
          })
          if (param.page) {
            commit('SET_NEWS_LIST', state.news_list.concat(response.data.list))
          } else {
            commit('SET_NEWS_LIST', response.data.list)
          }
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetNewsView({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=news&a=donews_view', param)
        .then(response => {
          commit('SET_NEWS_VIEW', response.data)
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetProductList({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=product&a=doproduct_list', param)
        .then(response => {
          if (param.page) {
            commit('SET_PRODUCT_LIST', state.product_list.concat(response.data.list))
          } else {
            commit('SET_PRODUCT_LIST', response.data.list)
          }
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetProductView({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=product&a=doproduct_view', param)
        .then(response => {
          commit('SET_PRODUCT_VIEW', response.data)
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetSidebar({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=comm&a=dosidebar', param)
        .then(response => {
          commit('SET_SIDEBAR', response.data)
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  GetSubcolumnNav({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=comm&a=docolumn_nav', param)
        .then(response => {
           response.data.map(item=>{
            if(item.children){
              item.value=[]
              item.children.map((e,index)=>{
                e.value=index
              })
            }
          }) 
          commit('SET_SUBCOLUMN_NAV', response.data)
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  SubmitMessage({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=introduction&a=domessage_add', param)
        .then(response => {
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  SubmitFeedback({ commit, state }, param) {
    return new Promise((resolve, reject) => {
      wxPost('index.php?n=met_wxapp&c=introduction&a=dofeedback_add', param)
        .then(response => {
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  }
}
export default actions
